/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package ServiceLayer;

import Buslog.Calendar;
import Buslog.Event;
import Buslog.SuperUser;
import java.awt.Dimension;
import java.sql.ResultSet;
import javax.swing.JOptionPane;

/**
 *
 * @author degrave
 */
public class NewFacade {

    static SuperUser userMain;
    static Event event;
    static Calendar calendar;

    public static int authorize(String login, String password) {
        userMain = SuperUser.findUser(login);
        if (userMain.getName().equals("") & userMain.getPassword().equals("") & userMain.gettableId() == 0) {
            return 0;
        }
        if (userMain.getPassword().equals(password)) {
            int whoIam = userMain.getPermission();
            return whoIam;
        }
        if (!userMain.getPassword().equals(password)) {
            return 0;
        }
        return 0;
    }

    public static void logOut() {
        userMain.setPermission(3);
    }
    //id generator

    public static int newUserId() {
        int id;
        id = userMain.getMaxId();
        if (id == 0) {
            JOptionPane.showMessageDialog(null, "Cant find user table");
            return 0;
        }
        return id + 1;
    }

    public static int newtableId() {
        int id = userMain.getMaxTableId();
        if (id == 0) {
            JOptionPane.showMessageDialog(null, "Cant find calendar table");
            return 0;
        }
        return id + 1;
    }

    public static int newEventId() {
        int id = Event.getMaxId();
        if (id == 0) {
            JOptionPane.showMessageDialog(null, "Cant find event table");
            return 0;
        }
        return id + 1;
    }
    //users

    public static void addUser(SuperUser user) {
        user.pushToDB();
    }

    public static SuperUser findUser(String name) {
        return SuperUser.findUser(name);
    }

    public static ResultSet findAllUsers() {
        return SuperUser.findAllUsers();
    }

    public static void removeUser(String name) {
        userMain = SuperUser.findUser(name);
        userMain.removeFromDB();
    }
    //events

    public static void addEvent(Event event) {
        event.pushToDB();
    }

    public static Event findEvent(int eventId) {
        return Event.findEvent(eventId);
    }

    public static ResultSet findEvent(String dateOrTime, int key) {
        return Event.findEvent(dateOrTime, key);
    }

    public static ResultSet findAllEvents(int owner) {
        return Event.findAllEvents(owner);
    }

    public static void removeEvent(String date, String time) {
        Event.removeFromDB(date, time);
    }

    public static void removeAllEvent(int ownerid) {
        Event.removeFromDB(ownerid);
    }
    //calendars

    public static void pushToCalendarDB(Calendar cal) {
        cal.pushToDB();
    }

    public static void removeCalendarFromDB(Calendar cal) {
        cal.removeFromDB();
    }

    public static Calendar findCalendar(int id, int key) {
        return Calendar.findCalendar(id, key);
    }

    public static int getMaxCalendarId() {
        return Calendar.getMaxId();
    }

    public static Dimension setPosition(Dimension screenSize, Dimension frameSize) {
        if (frameSize.height > screenSize.height) {
            frameSize.height = screenSize.height;
        }
        if (frameSize.width > screenSize.width) {
            frameSize.width = screenSize.width;
        }
        Dimension toReturn = new Dimension();
        int width = (screenSize.width - frameSize.width) / 2;
        int height = (screenSize.height - frameSize.height) / 2;
        toReturn.setSize(width, height);
        return toReturn;
    }
}
